package cn.tedu._04mvcweibo.interceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

/**
 * 编写拦截器
 */
@Slf4j
public class TimeAccessInterceptor implements HandlerInterceptor {
    /**目标Controller方法执行之前执行*/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("TimeAccessInterceptor.preHandle");
        //业务实现9:00~18:00可以
        LocalTime now = LocalTime.now();//JDK8中可以通过LocalTime获取时间
        int hour = now.getHour();
        System.out.println("hour="+hour);
        if(hour<9||hour>18)
            throw new RuntimeException("请在9~18之间进行注册");
        return true;//false表示请求到此结束,true表示放行
    }
}
